package components.editor
{
	import UIFree.uif_bitmap;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	import UIUtils.uif_utilBitmap;
	
	import classes.c_locale;
	import classes.c_util;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.PixelSnapping;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class c_bitmapElement extends c_baseElement {
		private var _bitmap		: Bitmap;
		private var _source		: DisplayObject;
		private var _holeMask	: c_hole;
		
		public function c_bitmapElement(parent: uif_component, rect: Rectangle, source: DisplayObject) {
			_source = source;
			super(parent, rect.x, rect.y, rect.width, rect.height);
		}
		
		override protected function createComponents():void {
			super.createComponents();
			assignSource(_source);
		}
		
		override protected function nameClass(): String {
			return locale.BITMAP;
		}
		
		public function assignSource(source: DisplayObject): void {
			_bitmap = new Bitmap(uif_utilBitmap.copyBitmapData(source));
			_bitmap.smoothing = true;
			addChild(_bitmap);
			scale = width/_bitmap.width;
			resize(_bitmap.width, _bitmap.height);
		}
		
		override public function set mask(value:DisplayObject):void {
			if (value is c_hole) setHoleMask(value as c_hole);
			else super.mask = value;
		}
		
		override public function get hitMouse(): Boolean {
			return stage?hitTest(stage.mouseX, stage.mouseY):false;
		}
		
		public function hitTest(stageX: Number, stageY: Number): Boolean {
			var p: Point = _bitmap.globalToLocal(new Point(stageX, stageY));
			return _bitmap.bitmapData.hitTest(uif_util.NULLPOINT, 0x33, p); 
		}
		
		protected function setHoleMask(a_hole: c_hole): void {
			if (a_hole != _holeMask) {
				if (_holeMask) removeEventHoleMask();
				_holeMask = a_hole;
				if (_holeMask) listenEventHoleMask();
			}
			updateFromMask();
//			var p: Point = a_hole.parent.localToGlobal(a_hole.position);	
		}
		
		override protected function setMatrix(a_value: Matrix): void {
			super.setMatrix(a_value);	
			updateFromMask();
		}
		
		override protected function doSetPosition(a_x:Number, a_y:Number):void {
			super.doSetPosition(a_x, a_y);
			updateFromMask();
		}
		
		private function listenEventHoleMask(): void {
			_holeMask.addEventListener(Event.REMOVED_FROM_STAGE, doRemoveHole);
		}
	
		private function removeEventHoleMask(): void {
			_holeMask.removeEventListener(Event.REMOVED_FROM_STAGE, doRemoveHole);
		}
		
		private function doRemoveHole(e: Event): void {
			removeEventHoleMask();
		}
		
		override public function dispose():void {
			if (_holeMask) removeEventHoleMask();
			super.dispose();
		}
		
/*		
		private function get sourceBitmapData(): BitmapData {
			if (_source is Loader) return ((_source as Loader).contentLoaderInfo.content as Bitmap).bitmapData
			else if (_source is Bitmap) return (_source as Bitmap).bitmapData;
			
			var btmp: BitmapData = new BitmapData(_source.width, _source.height, true, 0);
			btmp.draw(_source);
			return btmp;
		}
		
		protected function getHoleBitmapData(): BitmapData {
			var mat		: Matrix = new Matrix();
			mat.scale(scale, scale);
			mat.scale(_holeMask.scaleX, _holeMask.scaleY);
			
			var size: Point = _holeMask.size;
			size = mat.transformPoint(size);
			
			var result	: BitmapData = new BitmapData(size.x, size.y);
			return result;
		}
*/		
		
		protected function copySourceFromBitmap(): void {
			_bitmap.bitmapData.fillRect(_bitmap.bitmapData.rect, 0);
			if (_source as Loader) {
				var bmd: BitmapData = ((_source as Loader).contentLoaderInfo.content as Bitmap).bitmapData;
				_bitmap.bitmapData.copyPixels(bmd, bmd.rect, c_util.POINT0); 
			} else _bitmap.bitmapData.draw(_source);
			_bitmap.smoothing = true;
		}
		
		protected function updateFromMask(): void {
			if (_bitmap) {
				if (_holeMask) {
					var mat		: Matrix = _holeMask.transform.matrix.clone();
					mat.concat(c_util.invertMatrix(transform.matrix));
					
					//_bitmap.bitmapData.fillRect(_bitmap.bitmapData.rect, 0);
					copySourceFromBitmap();
					
					var atmp: BitmapData = new BitmapData(_bitmap.bitmapData.width, _bitmap.bitmapData.height, true, 0);
					atmp.draw(_holeMask, mat, null, BlendMode.NORMAL, null, true);
					_bitmap.bitmapData.copyChannel(atmp, atmp.rect, c_util.POINT0, BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
					_bitmap.smoothing = true;
					atmp.dispose();
					
				} //else copySourceFromBitmap();
			}
		}
		
		public function get bitmap(): Bitmap {
			return _bitmap;
		}
		
		override public function exportFromMLP(optimize:Boolean=false):DisplayObject {
			var bounds	: Rectangle;
			var bmp		: Bitmap;
			var mat		: Matrix;
			
			bounds = getBounds(parent);
			
			if (optimize) {
				mat = transform.matrix.clone();
				mat.translate(-bounds.x, -bounds.y);
				
				var btmp	: BitmapData = new BitmapData(bounds.width, bounds.height, true, 0);
				var btmp2	: BitmapData = new BitmapData(bounds.width, bounds.height, true, 0);
				btmp.draw(this, mat);
				btmp2.draw(this, mat, transform.colorTransform, blendMode);
				
				btmp.applyFilter(btmp, btmp.rect, uif_util.NULLPOINT, new BlurFilter(10, 10));
				btmp.threshold(btmp, btmp.rect, uif_util.NULLPOINT, '<', 0x22000000, 0, 0xFF000000);
				var rect: Rectangle = btmp.getColorBoundsRect(0xFF000000, 0, false);
				btmp.dispose();
				bounds.x += rect.x;
				bounds.y += rect.y;
				
				bmp = new Bitmap(btmp2, PixelSnapping.AUTO, true); 
				bmp.x = bounds.x;
				bmp.y = bounds.y;
			} else return super.exportFromMLP();
				
			return bmp;
		}
	}
}